/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.integral.task;

import cn.hutool.core.collection.CollectionUtil;
import com.medusa.gruul.integral.api.entity.StepNumber;
import com.medusa.gruul.integral.service.IStepNumberService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 每日已兑换步数进行清空
 *
 * @Author: xiaoq
 * @Date : 2019/12/24 9:10
 */
@JobHandler(value = "stepNumberEmptyJob")
@Component
@Slf4j
public class StepNumberEmptyJob extends IJobHandler {

	@Resource
	private IStepNumberService stepNumberService;
	private final Logger logger = LoggerFactory.getLogger(getClass());

	@Override
	public ReturnT<String> execute(String s) throws Exception {
		XxlJobLogger.log("XXL-JOB, Hello World.");
		logger.debug("执行修改用户每日已兑换步数为0任务");
		List<StepNumber> stepNumbers = stepNumberService.selectByHasChange();
		log.info("receive message:" + stepNumbers.toString());
		if (CollectionUtil.isNotEmpty(stepNumbers)) {
			stepNumbers.stream().forEach(sn -> {
				sn.setHasChange(0);
				try {
					stepNumberService.updateStepNumber(sn);
				} catch (Exception e) {
					log.error("需要更新的ids:".concat(stepNumbers.toString()) + e.getMessage(), e);
				}
			});
		}
		log.info("-----------改用户每日已兑换步数为0定时任务结束-----------");
		return SUCCESS;
	}
}
